﻿using PlayerRoles;
using PluginAPI.Core;
using System;
using System.Collections.Generic;
using System.Linq;



namespace RoundTicks
{
    public class TeamRoles
    {
        public RoleVote SCP049 { get; set; } = new RoleVote(RoleTypeId.Scp049);
        public RoleVote SCP0492 { get; set; } = new RoleVote(RoleTypeId.Scp0492);
        public RoleVote SCP079 { get; set; } = new RoleVote(RoleTypeId.Scp079);
        public RoleVote SCP096 { get; set; } = new RoleVote(RoleTypeId.Scp096);
        public RoleVote SCP106 { get; set; } = new RoleVote(RoleTypeId.Scp106);
        public RoleVote SCP173 { get; set; } = new RoleVote(RoleTypeId.Scp173);
        public RoleVote SCP939 { get; set; } = new RoleVote(RoleTypeId.Scp939);
        public RoleVote SCP3114 { get; set; } = new RoleVote(RoleTypeId.Scp3114);

        public RoleVote Scientist { get; set; } = new RoleVote(RoleTypeId.Scientist);
        public RoleVote FacilityGuard { get; set; } = new RoleVote(RoleTypeId.FacilityGuard);
        public RoleVote ClassD { get; set; } = new RoleVote(RoleTypeId.ClassD);

        public TeamRoles() { }


        /// <summary>
        /// 投票
        /// </summary>
        /// <param name="role"></param>
        /// <param name="userID"></param>
        private void TickSCP(RoleTypeId role, string userID)
        {
            this.RemoveUser(userID);

            switch (role)
            {
                case RoleTypeId.Scp173:
                    this.SCP173.RoleVoteUserID.Add(userID);
                    break;
                case RoleTypeId.Scp106:
                    this.SCP106.RoleVoteUserID.Add(userID);
                    break;
                case RoleTypeId.Scp049:
                    this.SCP049.RoleVoteUserID.Add(userID);
                    break;
                case RoleTypeId.Scp079:
                    this.SCP079.RoleVoteUserID.Add(userID);
                    break;
                case RoleTypeId.Scp096:
                    this.SCP096.RoleVoteUserID.Add(userID);
                    break;
                case RoleTypeId.Scp0492:
                    this.SCP0492.RoleVoteUserID.Add(userID);
                    break;
                case RoleTypeId.Scp939:
                    this.SCP939.RoleVoteUserID.Add(userID);
                    break;
                case RoleTypeId.Scp3114:
                    this.SCP3114.RoleVoteUserID.Add(userID);
                    break;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="role"></param>
        /// <param name="userID"></param>
        private void TickHuman(RoleTypeId role, string userID)
        {
            this.RemoveUser(userID);

            if (role == RoleTypeId.ClassD)
            {
                this.ClassD.RoleVoteUserID.Add(userID);
                return;
            }

            if (role == RoleTypeId.Scientist)
            {
                this.Scientist.RoleVoteUserID.Add(userID);
                return;
            }

            if (role == RoleTypeId.FacilityGuard)
            {
                this.FacilityGuard.RoleVoteUserID.Add(userID);
                return;
            }
        }

        /// <summary>
        /// 移除
        /// </summary>
        /// <param name="userID"></param>
        private void RemoveUser(string userID)
        {
            this.SCP049.RoleVoteUserID.Remove(userID);
            this.SCP0492.RoleVoteUserID.Remove(userID);
            this.SCP079.RoleVoteUserID.Remove(userID);
            this.SCP096.RoleVoteUserID.Remove(userID);
            this.SCP106.RoleVoteUserID.Remove(userID);
            this.SCP173.RoleVoteUserID.Remove(userID);
            this.SCP939.RoleVoteUserID.Remove(userID);
            this.SCP3114.RoleVoteUserID.Remove(userID);

            this.FacilityGuard.RoleVoteUserID.Remove(userID);
            this.Scientist.RoleVoteUserID.Remove(userID);
            this.ClassD.RoleVoteUserID.Remove(userID);
        }

        /// <summary>
        /// 投票
        /// </summary>
        public void TickRole(string arg0, string userID)
        {
            switch (arg0)
            {
                case "科学家":
                    this.TickHuman(RoleTypeId.Scientist, userID);
                    return;
                case "设施警卫":
                    this.TickHuman(RoleTypeId.FacilityGuard, userID);
                    return;
                case "D级人员":
                    this.TickHuman(RoleTypeId.ClassD, userID);
                    return;
                default:
                    break;
            }

            RoleTypeId roleType;

            switch (arg0)
            {
                case "049":
                    roleType = RoleTypeId.Scp049;
                    break;
                case "079":
                    roleType = RoleTypeId.Scp079;
                    break;
                case "096":
                    roleType = RoleTypeId.Scp096;
                    break;
                case "106":
                    roleType = RoleTypeId.Scp106;
                    break;
                case "173":
                    roleType = RoleTypeId.Scp173;
                    break;
                case "939":
                    roleType = RoleTypeId.Scp939;
                    break;
                case "3114":
                    roleType = RoleTypeId.Scp3114;
                    break;
                case "0492":
                default:
                    roleType = RoleTypeId.Scp0492;
                    break;
            }

            this.TickSCP(roleType, userID);
        }


        public override string ToString()
        {
            return $"<color=red>SCP-049</color>[{this.SCP049.RoleVoteCount}] | "
                + $"<color=red>SCP-079</color>[{this.SCP079.RoleVoteCount}] | "
                + $"<color=red>SCP-096</color>[{this.SCP096.RoleVoteCount}] | "
                + $"<color=red>SCP-106</color>[{this.SCP106.RoleVoteCount}] | "
                + $"<color=red>SCP-173</color>[{this.SCP173.RoleVoteCount}] | "
                + $"<color=red>SCP-939</color>[{this.SCP939.RoleVoteCount}] | "
                + $"<color=red>SCP-3114</color>[{this.SCP3114.RoleVoteCount}]\n\n"
                + $"<color=yellow>科学家</color>[{this.SCP049.RoleVoteCount}] | "
                + $"<color=#CECECE>设施警卫</color>[{this.SCP049.RoleVoteCount}] | "
                + $"<color=Orange>D级人员</color>[{this.SCP049.RoleVoteCount}] ";
        }


        public List<RoleTypeId> GetRole()
        {
            int maxCount = Player.Count;

            int[] random = new int[maxCount];

            for (int i = 0; i < maxCount; i++)
            {
                random[i] = new Random().Next(0, 100);
            }

            return new List<RoleTypeId>();
        }


        void FillArray(char[] array, int groupSize)
        {
            int index = 0;
            int groups = RoundTickLoad.Singleton.Config.GetCount() / groupSize; // 计算可以完整填充的组数  



            for (int i = 0; i < groups; i++)
            {
                // 填充每组  
                array[index++] = 'A';
                array[index++] = 'A';
                array[index++] = 'A';
                array[index++] = 'B';
                array[index++] = 'B';
                array[index++] = 'C';
                array[index++] = 'D';
            }

            // 填充剩余空间  
            while (index < array.Length)
            {
                // 这里简单起见，我们只用'A'来填充剩余空间  
                // 你可以根据需要调整填充策略  
                array[index++] = 'A';
            }
        }
    }
}
